package util;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;

import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;

/**
 * Many Spring Context Loader (for example ClassPathXmlApplicationContext ) are subclass of DefaultResourceLoader.
	DefaultResourceLoader has a constructor where you can specify the Classloader and also has a setClassLoader method.
	So it is your task to find a constructor of the Spring Context Loader you need, where you can specify the classloader, or just create it, and then use the set to set the classloader you want.
 * @author Christophe
 *
 */
/**
 * http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/context/support/ClassPathXmlApplicationContext.html
 * http://stackoverflow.com/questions/1691866/getting-an-application-context
 * 
 * I suggest you have a look at the org.springframework.context.support.GenericApplicationContext.
 * Together with an org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 * It should give you the flexibility you want.
 * There is an example of code on the GenericApplicationContext's javadoc
	Your code would look like:

	GenericApplicationContext ctx = new GenericApplicationContext();
	XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
	xmlReader.loadBeanDefinitions(new ClassPathResource("classpathContext.xml"));
	xmlReader.loadBeanDefinitions(new FileSystemResource("fileSystemContext.xml"));

	ctx.refresh();

 	MyBean myBean = (MyBean) ctx.getBean("myBean");

	For the typical case of XML bean definitions, simply use ClassPathXmlApplicationContext or FileSystemXmlApplicationContext, which are easier to set up - but less flexible, since you can just use standard resource locations for XML bean definitions, rather than mixing arbitrary bean definition formats. The equivalent in a web environment is XmlWebApplicationContext.
 */
/**
	public class FileSystemResource
	extends AbstractResource
	implements WritableResource
	Resource implementation for java.io.File handles. Obviously supports resolution as File, and also as URL. Implements the extended WritableResource interface.
 */

/**
	public class ClassPathResource
	extends AbstractResource
	Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources.
	Supports resolution as java.io.File if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL.
 */
public class ApplicationContextClassLoader {


	public ClassLoader getClassLoader(String location) throws Exception {
		GenericApplicationContext ctx = new GenericApplicationContext();

		XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);

		xmlReader.loadBeanDefinitions(new ClassPathResource("context.xml"));
		xmlReader.loadBeanDefinitions(new FileSystemResource("fileSystemContext.xml"));
		return null;
	}

	/*
	public ClassLoader getClassLoader(String location) throws Exception {
		File f = new File(location);
		return new URLClassLoader(new URL[]{f.toURI().toURL()});
	}

	public ApplicationContext getContext(String location,String... contextPaths) throws Exception {
		return getContext(null,location,contextPaths);
	} 

	public ApplicationContext getContext(ApplicationContext parentContext,String location,String... contextPaths) throws Exception {
		//init
		GenericApplicationContext context = new GenericApplicationContext(); 
		//build the app context
		if (parentContext != null) {
			context = new GenericApplicationContext(parentContext);
		}//end if
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);
		//get the classloader
		ClassLoader loader = getClassLoader(location);
		//set the loader
		reader.setBeanClassLoader(loader);
		reader.setResourceLoader(new DefaultResourceLoader(loader));
		//get the bean definitions
		reader.loadBeanDefinitions(contextPaths);
		//init
		context.refresh();
		//return
		return context;
	}
	 */

}